import axios from "axios"
import { Notify, showNotify } from "vant"
// import { useRouter } from "vue-router"
import router from "@/router/router"
const http = axios.create({
  baseURL:
    process.env.NODE_ENV === "production" ? "http://backend-api-01.newbee.ltd/api/v1/" : "/api/v1",
  timeout: 5000,
})

http.interceptors.request.use(
  config => {
    config.headers.token = localStorage.getItem("token") || ""
    return config
  },
  err => {
    return Promise.reject(err)
  }
)
http.interceptors.response.use(
  res => {
    if (res.data.resultCode !== 200) {
      if (res.data.resultCode === 416) {
        //根据接口定义的响应状态码 进行判断, 如果当前是登录页面不用跳转登录页 否则自动跳转登录页面
        if (router.currentRoute.path != "/login") {
          router.push("/login?needback=true")
        }
      }
      showNotify({
        type: "danger",
        message: res.data.message || "系统繁忙",
      })
    }

    return res.data
  },
  err => {
    showNotify({
      type: "danger",
      message: "系统繁忙,稍后再试",
    })
    return Promise.reject(err)
  }
)
export default http
